<cfcomponent output="false">
	<cfset $ = application.helpers />
	
	<!--- --->
	
	<cffunction name="saveUser" access="public" output="true" returntype="void">
		<cfargument name="user_id" required="true" />
		
		#application#

	</cffunction>
	
	<!--- --->
	
	<cffunction name="createUser" access="public" output="true" returntype="string">
		<cfargument name="email" required="true" />
		<cfargument name="password" required="true" />
		<cfargument name="first_name" required="true" />
		<cfargument name="last_name" required="true" />
		<cfargument name="middle_name" required="false" default="" />
		<cfargument name="street_1" required="false" default="" />
		<cfargument name="street_2" required="false" default="" />
		<cfargument name="city" required="false" default="" />
		<cfargument name="state_id" required="false" default="" />
		<cfargument name="zip" required="false" default="" />
			
		<cfset var local = {} />
		
		<cfset structDelete(arguments,"password2") />
		<cfset structDelete(arguments,"email2") />
		
		<!--- encrypt password --->
		<cfset arguments.password = $.format.encrypt(arguments.password) />
		
		<cfquery name="local.dupeCheck" datasource="fundrdb">
			select hex(id) as id from user where email = #$.database.string(arguments.email)#
			and active = 1
		</cfquery>
		
		<cfif local.dupeCheck.recordcount gt 0>
			<cfset session.message = "Account already exists for '#arguments.email#'" />
			<cfset session.user.id = -1 />
			<cfset local.id = -1 />
		<cfelse>
			<!--- new ID --->
			<cfset local.id = $.database.save(table="user",id="",argumentCollection=arguments) />

			<cfset login(arguments.email,arguments.password) />
			
			<cfset $.session.addMessage("User Created Successfully.") />
		</cfif>
		
		<cfreturn local.id />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="getUserByID" access="public" output="true" returntype="query">
		<cfargument name="userID" required="true" />

		<cfreturn application.g.userGateway.getUserByID(arguments.userID) />
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getUserByEmail" access="public" output="true" returntype="query">
		<cfargument name="email" required="true" />

		<cfreturn application.g.userGateway.getUserByEmail(arguments.email) />
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getUserByFacebookID" access="public" output="true" returntype="query">

		<cfreturn application.g.userGateway.getUserByFacebookID(argumentCollection=arguments) />
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="setSessionUser" access="public" output="true" returntype="struct">
		<cfargument name="userID" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.user = application.s.userService.getUserByID(arguments.userID) />

		<cfset local.favorites = application.s.userService.getFavorites(arguments.userID) />

		<cfset local.json = {} />
		<cfset local.json.id = local.user.id />
		<cfset local.json.name = local.user.first_name & ' ' & local.user.last_name />
		<cfset local.json.funds = '123.00' />
		<cfset local.json.payments = '321.00' />
		<cfset local.json.favorites= valueList(local.favorites.fund_id) />

		<cfset local.id = $.database.save(
			table="session",
			id=session.id,
			client_id=$.format.decrypt(cookie.clientID),
			user_id=local.user.id,
			json=serializeJSON(local.json)
		) />
		
		<cfreturn session.user />
			
	</cffunction>
	
	<!--- --->
		
	<cffunction name="getSessionUser" access="public" output="true" returntype="struct">
		
		<cfset var local = {} />
		
		<cfreturn session.user />
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="logout" access="public" output="true" returntype="boolean">

		<cfset local.id = $.database.delete(
			table="session",
			id=session.id
		) />
		
		<cfreturn true />
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="login" access="public" output="true" returntype="boolean">
		<cfargument name="email" required="true" />
		<cfargument name="password" required="true" />

		<cfset var local = {} />
		
		<cfset local.user = application.g.userGateway.login(argumentCollection=arguments) />

		<cfif local.user.recordcount eq 1>
			<cfset setSessionUser(userID=local.user.id) />
			<cfreturn true />
		<cfelse>
			<cfreturn false />
		</cfif>
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getFavorites" access="public" output="true" returntype="query">
		<cfargument name="userID" required="true" />

		<cfreturn application.g.userGateway.getFavorites(arguments.userID) />
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="addFavorite" access="public" output="true" returntype="boolean">
		<cfargument name="fundID" required="true" />

		<cfset var local = {} />
		
		<cfif session.loggedin>
			<cfset $.database.save(
				table="fund_favorite",
				id="",
				fund_id=arguments.fundID,
				user_id=session.user.id
			) />
			
			<cfset setSessionUser(session.user.id) />
		</cfif>
		
		<cfreturn true />
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="removeFavorite" access="public" output="true" returntype="boolean">
		<cfargument name="fundID" required="true" />

		<cfset var local = {} />
		
		<cfif session.loggedin>
			<cfset application.g.userGateway.removeFavorite(arguments.fundID) />
			<cfset setSessionUser(session.user.id) />
		</cfif>
		
		<cfreturn true />
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="facebookConnect" access="public" output="true" returntype="string">
		<cfargument name="uid" required="true" />
		<cfargument name="accessToken" required="true" />

		<cfset var local = {} />
		
		<cftry>
			<cfhttp method="get" url="https://graph.facebook.com/#arguments.uid#?fields=id,first_name,last_name,name,email&access_token=#arguments.accessToken#">
			</cfhttp>
			
			<cfset local.result = deserializeJSON(cfhttp.fileContent) />
			
			<cfcatch>
				<cfset local.result = {} />
				<cfset local.result.error = 'Fundr error' />
			</cfcatch>
		</cftry>
		
		<!--- if access token checks out --->
		<cfif structKeyExists(local.result,'id') and local.result.id eq arguments.uid 
				and structKeyExists(local.result,'first_name')  
				and structKeyExists(local.result,'email')
				and structKeyExists(local.result,'last_name')
				and structKeyExists(local.result,'name')
		>
			<!--- if account exists in fundr --->
			<cfset local.user = getUserByFacebookID(local.result.id) />
			
			<cfif local.user.recordcount eq 0>
				<cfset local.user = getUserByEmail(local.result.email) />
				<cfif local.user.recordcount gt 0>
					<cfset $.database.save(
						table="user",
						id=local.user.id,
						fb_id=local.result.id		
					) />
				</cfif>
			</cfif>
			
			<!--- else create account and associate with facebook --->
			<cfif local.user.recordcount eq 0>
				<cfset createUser(
					email=local.result.email,
					password=RandRange(1000000, 9999999) & 'R',
					first_name=local.result.first_name,
					last_name=local.result.last_name
				) />
			<cfelse>
				<cfset login(email=local.user.email,password=local.user.password) />
			</cfif>
			
			<cfreturn true />
		<cfelse>
			<cfreturn false />
		</cfif>
		
		
		
		<cfreturn local.result />
			
	</cffunction>
	
	<!--- --->

</cfcomponent>